<template>
  <div class="order-manager">
    <el-form :model="form" label-position="right" class="class-form">
      <el-row :gutter="20">
        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4">
          <el-form-item label="Sku">
            <el-input
              v-model="form.sku"
              style="width: 100%"
              clearable
              placeholder="sku"
              prefix-icon="el-icon-search"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4">
          <el-form-item :label="$t('query.开始结束时间')">
            <el-date-picker
              v-model="form.date"
              type="daterange"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              style="width: 100%"
              :start-placeholder="$t('query.开始时间')"
              :end-placeholder="$t('query.结束时间')"
              clearable
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4">
          <el-form-item>
            <el-button
              type="primary"
              @click="
                page.start = 1;
                getOrderList(1);
              "
              >{{ $t("form.queryBtn") }}</el-button
            >
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-form :model="form" label-position="right" class="class-form">
      <el-row>
        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4">
          <el-form-item :label="$t('query.订单号id')">
            <el-input
              v-model="form_2.OrderId"
              style="width: 185px"
              clearable
              :placeholder="$t('query.订单号id')"
              prefix-icon="el-icon-search"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4">
          <el-form-item>
            <el-button
              type="primary"
              @click="
                page.start = 1;
                getOrderList(2);
              "
              >{{ $t("form.queryBtn") }}</el-button
            >
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <div class="table-container">
      <el-table :data="tableData" border>
        <el-table-column prop="OrderId" label="Order id" align="center">
        </el-table-column>
        <el-table-column prop="ItemId" label="ITEM ID" align="center">
        </el-table-column>
        <el-table-column
          prop="ExpirationDate"
          :label="$t('order.结账日期')"
          align="center"
        >
        </el-table-column>
        <el-table-column prop="sku" :label="$t('order.映射SKU')" align="center">
        </el-table-column>
        <el-table-column prop="shop" :label="$t('order.店铺名')" align="center">
        </el-table-column>
        <el-table-column
          prop="DateCreated"
          :label="$t('order.下单时间')"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="currency"
          :label="$t('order.币种')"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="quantity"
          :label="$t('order.数量')"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="TotalAmount"
          :label="$t('order.总金额')"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="SaleFee"
          :label="$t('order.杂费')"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="freight"
          :label="$t('order.运费')"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="RealAmount"
          :label="$t('order.真实价格')"
          align="center"
        >
        </el-table-column>
      </el-table>
    </div>
    <div class="page">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.start"
        background
        :page-sizes="[10, 100, 200, 300, 400]"
        :page-size="page.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { orderListApi_1, orderListApi_2 } from "@/api";
import { cloneDeep } from "loadsh";
export default {
  name: "order-manager",
  data() {
    return {
      form: {
        sku: "",
        StartDate: "",
        EndDate: "",
        date: [],
      },
      form_2: {
        OrderId: "",
      },
      queryKey: "",
      tableData: [],
      page: {
        start: 1,
        limit: 10,
      },
      total: 0,
    };
  },
  created() {
    this.getOrderList();
  },
  methods: {
    async getOrderList(type = 1) {
      let params = cloneDeep({
        ...this.page,
        start: this.page.start - 1,
      });
      if (type == 1) {
        const values = Object.keys(this.form).map((key) => this.form[key]);
        if (values.every((item) => !item)) {
          type = 3;
        } else {
          // params = Object.assign({}, params, cloneDeep(this.form));
          if (this.form.date) {
            this.form.StartDate = this.form.date[0];
            this.form.EndDate = this.form.date[1];
          } else {
            this.form.StartDate = "";
            this.form.EndDate = "";
          }
          Object.keys(this.form).map((key) => {
            if (this.form[key] && key != "date") {
              params[key] = this.form[key];
            }
          });
          const { data, total } = await orderListApi_1(params);
          this.tableData = data;
          this.total = total;
        }
      }
      if (type == 2) {
        if (this.form_2.OrderId) {
          params["OrderId"] = this.form_2.OrderId;
          const { data, total } = await orderListApi_1(params);
          this.tableData = data;
          this.total = total;
        } else {
          type = 3;
        }
      }
      // if (type == 3) {
      //   const { data, total } = await orderListApi_1(params);
      //   this.tableData = data;
      //   this.total = total;
      // }
    },
    handleSizeChange(e) {
      this.page.limit = e;
      this.getOrderList();
    },
    handleCurrentChange(e) {
      this.page.start = e;
      this.getOrderList();
    },
  },
};
</script>

<style lang="scss" scoped src="./order-manager.scss"></style>
